package com.flycms.core.entity;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 接口返回数据的基类
 */
public class RestDataVo implements Serializable {

    private static final long serialVersionUID = 1L;

    private String msg;// 返回信息
    private int code; // 状态码
    private Object data;// 数据内容

    public RestDataVo(String msg, RestStatusCode statusCode, Object data) {
        this.data = data;
        this.code = statusCode.getStatusCode();
        this.msg = msg;
    }

    public RestDataVo() {

    }

    /**
     * 分页
     * 
     * @param data      数据对象
     * @param pageIndex 当前页
     * @param pageSize  每页数量
     * @param total     总数
     * @return
     */
    public static RestDataVo PAGE(Object data, int pageIndex, int pageSize, int total) {
        PageBean page = new PageBean();
        page.setPageIndex(pageIndex);
        page.setPageSize(pageSize);
        page.setTotal(total);
        Map<String, Object> map = new HashMap<>();
        map.put("list", data);
        map.put("page", page);
        return new RestDataVo(RestStatusCode.SUCCESS.getCodeName(), RestStatusCode.SUCCESS, map);
    }

    /**
     * 返回成功
     * 
     * @param data
     * @return 状态码 和 数据
     */
    public static RestDataVo OK(Object data) {
        return new RestDataVo(RestStatusCode.SUCCESS.getCodeName(), RestStatusCode.SUCCESS, data);
    }

    /**
     * 返回成功
     * 
     * @param message
     * @param data
     * @return
     */
    public static RestDataVo OK(String message, Object data) {
        return new RestDataVo(message, RestStatusCode.SUCCESS, data);
    }

    /**
     * 返回成功
     * 
     * @param message
     * @return 状态码 和 信息
     */
    public static RestDataVo OK(String message) {
        return new RestDataVo(message, RestStatusCode.SUCCESS, null);
    }

    /**
     * 返回失败
     * 
     * @param data
     * @return
     */
    public static RestDataVo ERROR(Object data) {
        return new RestDataVo(RestStatusCode.FAIL.getCodeName(), RestStatusCode.FAIL, data);
    }

    /**
     * 返回失败
     * 
     * @param statusCode
     * @return 状态码
     */
    public static RestDataVo ERROR(RestStatusCode statusCode) {
        return new RestDataVo(statusCode.getCodeName(), statusCode, null);
    }

    /**
     * 返回失败
     * 
     * @param data
     * @param statusCode
     * @return
     */
    public static RestDataVo ERROR(Object data, RestStatusCode statusCode) {
        return new RestDataVo(null, statusCode, data);
    }

    /**
     * 返回失败带错误信息提示
     * 
     * @param message
     * @return
     */
    public static RestDataVo ERROR(String message) {
        return new RestDataVo(message, RestStatusCode.FAIL, null);
    }
    
    /**
     * 返回失败带错误信息提示
     * @param messagePattern 错误信息PATTERN
     *  <br/>类似 it's {0} OK, not ==> it's not OK
     * @param args
     * @return
     * @author 沁言訫語
     */
    
    public static RestDataVo ERROR(String messagePattern, Object... args) {
        String msg = MessageFormat.format(messagePattern, args);
        return new RestDataVo(msg, RestStatusCode.FAIL, null);
    }
    
    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}